﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HotelResSystem2
{
    public class ReservationSystem
    {
        List<Reservation> reservations = new List<Reservation>();
        private PaymentSystem paySys = PaymentSystem.GetInstance();
        private static ReservationSystem HotelResSysSingletonInstance;
        static readonly object padlock = new object();

        /// <summary>
        /// Private constructor for singleton pattern
        /// </summary>
        private ReservationSystem()
        {
            // Retrieve the list of reservations before loading
            Reservation[] resList = DataStorage.GetInstance().GetReservationList();
            if (resList.Length > 0)
            {
                reservations.AddRange(resList);
            }
        }

        public int TotalReservations
        {
            get { return reservations.Count; }
        }

        public static ReservationSystem GetInstance()
        {
            lock (padlock)
            {
                if (HotelResSysSingletonInstance == null)
                {
                    HotelResSysSingletonInstance = new ReservationSystem();
                }
                return HotelResSysSingletonInstance;
            }
        }

        public void createNewReservation(Reservation newRes)
        {
            if (newRes != null)
            {
                DataStorage.GetInstance().saveReservationInfo(newRes);
                reservations.Add(newRes);
            }
        }

        public void CheckIn(Reservation res)
        {
            DataStorage.GetInstance().updateReservationInfo(res);
        }

        public void CheckOut(Reservation res)
        {
            for (int i = 0; i < reservations.Count; i++)
            {
                if (reservations[i].ResID == res.ResID)
                {
                    DataStorage.GetInstance().removeReservationInfo(reservations[i]);
                    reservations.RemoveAt(i);
                    break;
                }
            }
        }

        public Reservation lookUpResByID(string resID)
        {
            foreach (Reservation res in reservations)
            {
                if (res.ResID == resID)
                {
                    return res;
                }
            }

            // No reservation found--return empty class
            return new Reservation();
        }

        public Reservation lookUpResByLastName(string lastName)
        {
            foreach (Reservation res in reservations)
            {
                if (res.PrimaryGuest.ContactInfo.LastName == lastName)
                {
                    return res;
                }
            }

            // No reservation found--return empty class
            return new Reservation();
        }
    }
}
